/**
 * Copyright (c) 2014-2018, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.leetcode;

/**
 * 给出两个 非空 的链表用来表示两个非负的整数。其中，它们各自的位数是按照 逆序 的方式存储的，并且它们的每个节点只能存储 一位 数字。
 * <p>
 * 如果，我们将这两个数相加起来，则会返回一个新的链表来表示它们的和。
 * <p>
 * 您可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 * <p>
 * 示例：
 * <p>
 * 输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
 * 输出：7 -> 0 -> 8
 * 原因：342 + 465 = 807
 *
 * @author Xue Xue (xuexue1@huli.com)
 * @version $Id$
 * @since 2019/3/7
 */
public class Q2 {

    /**
     * Definition for singly-linked list.
     */
    public static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int x) {
            val = x;
        }
    }

    static class Solution {
        public static ListNode MyAddTwoNumbers(ListNode l1, ListNode l2) {
            ListNode temp1 = l1;
            ListNode temp2 = l2;

            int sum = l1.val + l2.val;
            int mod = sum % 10;
            int div = sum / 10;
            ListNode l3 = new ListNode(mod);
            ListNode temp = l3;
            while ((temp1 != null && temp1.next != null) || (temp2 != null && temp2.next != null)) {
                int a = 0;
                int b = 0;
                if (temp1 != null && temp1.next != null) {
                    a = temp1.next.val;
                }
                if (temp2 != null && temp2.next != null) {
                    b = temp2.next.val;
                }
                sum = a + b + div;
                mod = sum % 10;
                div = sum / 10;
                ListNode nwNode = new ListNode(mod);
                temp.next = nwNode;
                temp = temp.next;
                if (temp1 != null && temp1.next != null) {
                    temp1 = temp1.next;
                }
                if (temp2 != null && temp2.next != null) {
                    temp2 = temp2.next;
                }
            }
            if (div > 0) {
                temp.next = new ListNode(1);
            }
            return l3;
        }

        public static ListNode MyAddTwoNumbers2(ListNode l1, ListNode l2) {
            int div = 0;
            ListNode l3 = null;
            ListNode temp = null;
            while (l1 != null || l2 != null) {
                int a = 0;
                int b = 0;
                if (l1 != null) {
                    a = l1.val;
                }
                if (l2 != null) {
                    b = l2.val;
                }
                int sum = a + b + div;
                int mod = sum % 10;
                div = sum / 10;
                ListNode nwNode = new ListNode(mod);
                if (l3 == null) {
                    l3 = nwNode;
                    temp = l3;
                } else {
                    temp.next = nwNode;
                    temp = temp.next;
                }

                if (l1 != null && l1.next != null) {
                    l1 = l1.next;
                } else {
                    l1 = null;
                }
                if (l2 != null && l2.next != null) {
                    l2 = l2.next;
                } else {
                    l2 = null;
                }
            }
            if (div > 0) {
                temp.next = new ListNode(1);
            }
            return l3;
        }

        public static ListNode RightAddTwoNumbers(ListNode l1, ListNode l2) {
            ListNode dummyHead = new ListNode(0);
            ListNode p = l1, q = l2, curr = dummyHead;
            int carry = 0;
            while (p != null || q != null) {
                int x = (p != null) ? p.val : 0;
                int y = (q != null) ? q.val : 0;
                int sum = carry + x + y;
                carry = sum / 10;
                curr.next = new ListNode(sum % 10);
                curr = curr.next;
                if (p != null) {p = p.next;}
                if (q != null) {q = q.next;}
            }
            if (carry > 0) {
                curr.next = new ListNode(carry);
            }
            return dummyHead.next;
        }
    }

    public static void main(String[] args) {
        //[2,4,3]&[5,4,6]
        ListNode a1 = new ListNode(2);
        ListNode a2 = new ListNode(4);
        ListNode a3 = new ListNode(3);
        a1.next = a2;
        a2.next = a3;

        ListNode b1 = new ListNode(5);
        ListNode b2 = new ListNode(4);
        ListNode b3 = new ListNode(6);
        b1.next = b2;
        b2.next = b3;
        ListNode c = Solution.RightAddTwoNumbers(a1, b1);
        ListNode temp = c;
        System.out.println(c.val);
        while (temp.next != null) {
            System.out.println(temp.next.val);
            temp = temp.next;
        }
    }

}
